// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// C [author] rhyme_lph
// E [email]  rhymelph@gmail.com
// W [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/utils/reg_exp_utils.dart';
import 'package:finance_app/src/widgets/common/expandable.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/list_item/list_expandable_item.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/all_reports/views/reports_provider.dart';
import 'package:finance_app/src/provider/all_reports/all_reports_provider.dart';

class ReportsView extends StatefulWidget {
  @override
  _ReportsViewState createState() => _ReportsViewState();
}

class _ReportsViewState extends State<ReportsView>
    with
        StatefulPresenter<ReportsView, AllReportsProvider, ReportsProvider>,
        KeepAliveMixin {
  @override
  Widget buildWidget(BuildContext context) => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            AppSpaces.h20,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
            AppSpaces.h20,
            listBuilder<String>(
              selector: (_, __) => __.searchController.text,
              builder: (BuildContext context, String value, Widget child) =>
                  _buildSearchWidget(value),
            ),
          ],
        ),
      );

  Widget _buildSearchWidget(String search) {
    List<Widget> children = [];
    if (Config.hasPermission(PermissionUtils.salesReport))
      children.add(_buildAddWidget(
        controller: provider.saleExp,
        title: S.current.sales,
        list: provider.saleList,
        searchText: search,
      ));
    if (Config.hasPermission(PermissionUtils.purchaseReports))
      children.add(_buildAddWidget(
        controller: provider.purchasesExp,
        title: S.current.purchases,
        list: provider.purchasesList,
        searchText: search,
      ));
    if (Config.hasPermission(PermissionUtils.inventoryReports))
      children.add(_buildAddWidget(
        controller: provider.inventoryExp,
        title: S.current.inventory,
        list: provider.inventory,
        searchText: search,
      ));

    if (Config.hasPermission(PermissionUtils.hrPayrollReports))
      children.add(_buildAddWidget(
        controller: provider.hrPayrollExp,
        title: S.current.hr_payroll,
        list: provider.hrPayrolls,
        searchText: search,
      ));
    if (Config.currentCompany.enableGst != 1) {
      provider.taxes.remove("GST F5");
      provider.taxes.remove("GST Details");
    }
    if (Config.hasPermission(PermissionUtils.taxesReports))
      children.add(_buildAddWidget(
        controller: provider.taxExp,
        title: S.current.taxes,
        list: provider.taxes,
        searchText: search,
      ));
    if (Config.hasPermission(PermissionUtils.accountantReports))
      children.add(_buildAddWidget(
        controller: provider.accountantsExp,
        title: S.current.accountant,
        list: provider.accountants,
        searchText: search,
      ));
    children.add(AppSpaces.h40);
    return Column(
      children: children,
    );
  }

  Widget _buildAddWidget(
      {ExpandableController controller,
      String title,
      List<String> list,
      String searchText}) {
    List<String> searchResult = list
        .where((e) => RegExpUtils.getSearchRegExp(searchText).hasMatch(e))
        .toList();
    if (searchResult.length > 0) {
      return ListExpandableItem(
        controller: controller,
        title: title,
        content: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: searchResult.map(buildItemWidget).toList(),
        ),
      );
    } else {
      return Container();
    }
  }

  Widget buildItemWidget(String e) => PressedOpacityButton(
        padding:
            EdgeInsets.symmetric(horizontal: getDp(35), vertical: getDp(12.5)),
        onPressed: () => provider.onNavDetail(e),
        child: Align(
          alignment: Alignment.centerLeft,
          child: Text(
            LanguageUtils.translateReport(e),
            style: TextStyle(
              color: aceBlueColor,
              fontSize: getSp(18),
              fontWeight: FontWeight.w700,
              height: 20 / 18,
            ),
          ),
        ),
      );
}
